#coding:utf8

from Units import *
from Util import Ponto,VetorPonto,InfoIteration
from Graphics import *
import Properties
import pygame

from math import e

screen = pygame.display.set_mode( (300,300) )
pygame.init()

'''
A = 0.0083
s = 3500.0
x0 = 500.0

var_x = 7000
var_y = 800

e1_x = pow( (var_xx0),2 )
e2_x = ( 1/pow(s,2) )*e1_x

indice_x = A*pow(e,e2_x)

e1_y = pow( (var_yx0),2 )
e2_y = ( 1/pow(s,2) )*e1_y

indice_y = A*pow(e,e2_y)
'''

var_x = 1100
var_y = 1000

'''
indice_x = 3/9000.0 + (8.39*1e8)* (9000var_x)
indice_y = 3/9000.0 + (8.39*1e8)* (9000var_y)
'''

def converte_pontos( pontos ):
	_pontos = []
	
	i = 0
	for pt in pontos:
		_pontos.append( Ponto( pt[0],pt[1],i ) )		
		i+=1
		
	return _pontos

img,rect = load_surface_from_image(Properties.imgCurva )

c = Curva( img.get_size() )

'''
pontos_teste = [ (183.242199069,174.367138508),  (185.924878085,177.857469667),  (189.045617494,178.930891448),  (193.338376704,178.908206447),  (196.431008958,180.581252676)    ]

pontos_dk = [ (0,0),  (0,0),(0.0610274930554*0.5,1.87243945057*0.5),  (0,0),  (0,0)    ]
'''

pontos_teste = [ (193.051128676,134.346430434),  (188.330800907,137.246233329),  (183.790852491,139.519462782),  (180.288068586,142.411396186),  (178.525203219,145.159866026),  (176.134603214,148.058107669),  (175.018368648,150.996440987),  (174.21427384,155.493855523),  (175.852530935,160.34361053),  (177.357999689,164.315904572),  (179.525554306,168.051310493),  (182.128520041,171.731784308),  (185.51757482,174.781828949),  (188.931627102,177.524061636),  (192.625303482,178.834615081),  (196.272410063,180.283147092),  (200.189004328,181.591370923),  (204.282327351,182.845629087),  (208.454147325,184.028757025),  (212.754738791,185.078589325),  (217.464621169,185.2870569),  (222.163312826,185.453932891),  (226.780752215,185.506558814),  (231.317402672,185.430353443),  (235.772836798,185.341309248),  (239.787204424,184.513550238),  (243.936547866,183.410165594),  (247.192319936,182.360936369),  (250.307004078,180.126080221),  (252.774295286,177.568417598),  (254.973241182,174.496422991),  (257.087017113,171.013007911),  (257.723125706,168.641906505),  (258.193621291,164.992779861),  (257.445196137,161.157170895),  (255.19321787,156.20719601),  (253.337992779,151.664855224),  (251.544322218,147.284481757),  (249.123559281,143.267913184),  (246.162958985,139.527160225),  (243.54698659,137.23572604),  (240.408770143,135.54635503),  (236.993647902,133.252591299),  (233.172062737,131.493610265),  (228.513024241,128.996094622),  (224.03762511,129.356507839),  (220.091901042,129.015821465),  (216.103486093,129.999634954),  (211.796686066,129.781851989),  (207.647933109,130.580911872),  (203.306051584,131.352344212),  (198.205058321,133.151817174) ]

'''
pontos_dk = [  (0.0653906073383,0.470458957098),  (0.0740533913448,0.17574256543),  (0.0411294113911,-0.0170726917075),  (-0.0275973738089,0.106087051786),  (-0.00477902092981,0.0126202902589),  (0.0437739497639,0.0180372901724),  (0.00860180491389,0.00202508341569),  (0.125459355758,0.0229424604372),  (0.0399419776109,0.117625982762),  (-0.0442841981239,0.0430263133524),  (0.00321407343403,0.0597329393973),  (0.0482890129188,0.152059916),  (0.0131453548456,0.0406185506154),  (0.0048733155642,0.146005287944),  (0.274353116189,0.0633903845988),  (0.411747220471,-0.00583862088533),  (0.364564059106,-0.0440504941523),  (0.202659989531,-0.0546757428444),  (0.0659802850657,-0.0532320137298),  (-0.00509526983639,-0.0475524131121),  (-0.0289982876972,-0.0641356106367),  (-0.0536644824027,-0.0835821065223),  (-0.075169929501,-0.0859472829893),  (-0.0854099321857,-0.0610181028425),  (-0.0737537735218,0.00883660181552),  (-0.037347724372,-0.105129701836),  (0.132801564587,-0.0168889937074),  (0.06298240882,0.120536958672),  (0.0354545487695,0.0176726732586),  (-0.00424029615578,0.000426168026507),  (0.00171029974974,-1.92637246001e-005),  (0.0961610590592,-0.00566757664232),  (0.0950017527072,0.127915320121),  (0.0657237319142,0.00358065427977),  (0.103425023101,0.0517364517999),  (0.066173672594,0.069161533745),  (0.0209287103169,-0.0243221285832),  (0.0133286421243,0.0130977988346),  (0.127356597709,0.0934186001322),  (0.0748267781871,-0.0205099810506),  (0.162407908139,0.0431675179838),  (0.0121822809837,0.0755268281953),  (0.000622135882012,0.0860994286595),  (0.0579038104911,0.112649470678),  (-0.057507476817,0.050682575253),  (0.0149536764401,0.115263255143),  (0.0109580817825,-0.0180194006827),  (0.05148129825,0.164297959425),  (-0.0159171260079,0.237456205633),  (-0.115288199726,0.291201306208),  (-0.202178793862,0.264709915994),  (-0.0854323126309,0.176607344553)   ]
'''


pontos_dk = [  (0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0.274353116189,0.0633903845988),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)  ]


c.pontos = converte_pontos( pontos_teste )

s = Snake( load_image(Properties.imgCurva), c )
s.comp_suavidade_flag = True
s.comp_continuidade_flag = True
s.comp_cor_flag = True

xk = VetorPonto( c.pontos )
dk = VetorPonto( converte_pontos( pontos_dk ) )

it = InfoIteration(0)
s.iteracao_atual = it

xk.atualizar( s.cor,None,it)

pt = xk.pontos[14]

a = s.calcular_comp_continuidade(pt,xk)
b = s.calcular_comp_suavidade(pt,xk)
c = s.calcular_comp_cor(pt)

d = Ponto( a[0]+b[0]-c[0],a[1]+b[1]-c[1],4 )
e = VetorPonto( [d] )

print "##### AUTOMATICO PONTO ANTES #####"
print "Continuidade: ",s.calcular_comp_continuidade(pt,xk)
print "Suavidade: ",s.calcular_comp_suavidade(pt,xk)
print "Cor: ", s.calcular_comp_cor(pt)
print "Continuidade + Suavidade + Cor = ", ( a[0]+b[0]-c[0],a[1]+b[1]-c[1] )
print "Soma Normal: ", e.normal()
print "Gradiente Continuidade: ", (a[0]*pt._alfa,a[1]*pt._alfa)
print "Gradiente Suavidade: ", (b[0]*pt._beta,b[1]*pt._beta)
print "Gradiente Cor: ", (c[0]*pt._gama[0],c[1]*pt._gama[1])
print "Gradiente Total: ", ( a[0]*pt._alfa+b[0]*pt._beta-c[0]*pt._gama[0],a[1]*pt._alfa+b[1]*pt._beta-c[1]*pt._gama[1] )
print "Alfa: ", pt._alfa
print "Beta: ", pt._beta
print "Gama: ", pt._gama
print "Funcional: ", s.calcular_funcional( xk )," = ", s.calcular_funcional( xk ).normal()

print "\n\n"

'''
pt_dk = Ponto(0.271588437794,0.106409644451,0)
pt_14 = Ponto(pt.x+pt_dk.x,pt.y+pt_dk.y,14)

xk.pontos[13] = pt_14
'''

xk = xk.somar_vetor( dk )
xk.atualizar(s.cor,None,it)
pt_14 = xk.pontos[14]

'''
xk_2 = xk.somar_vetor(dk)
xk_2.atualizar(s.cor,None,it)
'''

a = s.calcular_comp_continuidade(pt_14,xk)
b = s.calcular_comp_suavidade(pt_14,xk)
c = s.calcular_comp_cor(pt_14)

d = Ponto( a[0]+b[0]-c[0],a[1]+b[1]-c[1],4 )
e = VetorPonto( [d] )

f = s.calcular_derivada_comp_continuidade(pt_14,xk)
g = s.calcular_derivada_comp_suavidade(pt_14,xk)
h = s.calcular_derivada_comp_cor(pt_14,xk)

print "##### AUTOMATICO PONTO DEPOIS #####"
print "Continuidade: ",s.calcular_comp_continuidade(pt_14,xk)
print "Suavidade: ",s.calcular_comp_suavidade(pt_14,xk)
print "Cor: ", s.calcular_comp_cor(pt_14)
print "Continuidade + Suavidade + Cor = ", ( a[0]+b[0]-c[0],a[1]+b[1]-c[1] )
print "Soma Normal: ", e.normal()
print "Gradiente Continuidade: ", (f[0]*pt_14._alfa,f[1]*pt_14._alfa)
print "Gradiente Suavidade: ", (g[0]*pt_14._beta,g[1]*pt_14._beta)
print "Gradiente Cor: ", (h[0]*pt_14._gama[0],h[1]*pt_14._gama[1])
print "Gradiente Total: ", ( f[0]*pt_14._alfa+g[0]*pt_14._beta-h[0]*pt_14._gama[0],f[1]*pt_14._alfa+g[1]*pt_14._beta-h[1]*pt_14._gama[1] )
print "Alfa: ", pt._alfa
print "Beta: ", pt._beta
print "Gama: ", pt._gama
print "Funcional: ", s.calcular_funcional( xk )," = ", s.calcular_funcional( xk ).normal()








'''
for i in range(len(xk.pontos) ):
	if not( xk.pontos[i].x == xk_2.pontos[i].x and xk.pontos[i].y == xk_2.pontos[i].y ):
		print i
'''

'''

pontos_dk = [ (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)  (0,0)     ]

vetor_dk = VetorPonto( converte_pontos( pontos_dk ) )
grad = vetor_dk.multiplicar_escalar(1)

condicao_armijo_pt_1 = ( s.calcular_funcional(xk) ).normal()
condicao_armijo_pt_2 = ( grad.produto_escalar( vetor_dk ) ) 

print s.busca_linear(xk,grad,vetor_dk)

print s.calcular_funcional( xk.somar_vetor( vetor_dk.multiplicar_escalar(1) ) ).normal()
print "ARMIJO: ", condicao_armijo_pt_1 + condicao_armijo_pt_2 * ( Snake.SIGMA*1)
print s.calcular_funcional( xk.somar_vetor( vetor_dk.multiplicar_escalar(0.5) ) ).normal()
print "ARMIJO: ", condicao_armijo_pt_1 + condicao_armijo_pt_2 * ( Snake.SIGMA*0.5)
print s.calcular_funcional( xk.somar_vetor( vetor_dk.multiplicar_escalar(0.25) ) ).normal()
print "ARMIJO: ", condicao_armijo_pt_1 + condicao_armijo_pt_2 * ( Snake.SIGMA*0.25)
print s.calcular_funcional( xk.somar_vetor( vetor_dk.multiplicar_escalar(0.125) ) ).normal()
print s.calcular_funcional( xk.somar_vetor( vetor_dk.multiplicar_escalar(0.0625) ) ).normal()
print s.calcular_funcional( xk.somar_vetor( vetor_dk.multiplicar_escalar(0.03125) ) ).normal()
print s.calcular_funcional( xk.somar_vetor( vetor_dk.multiplicar_escalar(0.015625) ) ).normal()
print s.calcular_funcional( xk.somar_vetor( vetor_dk.multiplicar_escalar(0.0078125) ) ).normal()
print s.calcular_funcional( xk.somar_vetor( vetor_dk.multiplicar_escalar(0.00390625) ) ).normal()
print s.calcular_funcional( xk.somar_vetor( vetor_dk.multiplicar_escalar(0.001953125) ) ).normal()
print s.calcular_funcional( xk.somar_vetor( vetor_dk.multiplicar_escalar(0.0009765625) ) ).normal()
'''